package com.example.touch;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.util.LogPrinter;
import android.view.MotionEvent;

public class Screen 
{	
	List<Area> areas;
	int count;
	String description; 
	
	public Screen(final String desc)
	{
		description = desc;
		areas = new ArrayList<Area>();
		count = 0;
	}
	public void addArea(float x, float y, float width, float height, String desc)
	{
		addArea(x, y, width, height, desc, "");
	}
	public void addArea(float x, float y, float width, float height, String desc, String callbackName)
	{
		Area a = new Area(x, y, width, height, desc);
		a.setId(++count);
		a.setCallback(callbackName);
		areas.add(a);
	}
	public void removeArea(String desc)
	{
		for(Area it : areas)
		{
			if(it.getDescription() == desc)
			{
				areas.remove(it);
			}
		}
	}
	public void removeArea(int areaId)
	{
		for(Area it : areas)
		{
			if(it.getId() == areaId)
			{
				areas.remove(it);
			}
		}
	}
	public void onTouch(MotionEvent e)
	{
		final int action = e.getAction();
		switch(action)
		{
		case MotionEvent.ACTION_DOWN:
		{
			float x, y;
			x = e.getX(0);
			y = e.getY(0);
			for(Area it : areas)
			{
				if(it.isInArea(x, y))
				{
					it.execute();
					LogPrinter log = new LogPrinter(Log.DEBUG, "log");
					log.println(it.getDescription());
				}
			}
			break;
		}
		case MotionEvent.ACTION_MOVE:
		{
			//invalidate();
			break;
		}
		}
	}
	static public  void setLogic(Logic l)
	{
		Area.setLogic(l);
	}
}
